/**
 * 神州信息(2012)
 */
package net.abbp.report.export;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import net.abbp.core.util.StringUtils;
import net.abbp.report.model.Cell;
import net.abbp.report.model.ReportModel;
import net.abbp.report.model.cell.style.HorizentalAlignment;
import net.abbp.report.model.cell.style.Style;
import net.abbp.report.view.cell.value.ImageValue;

import org.apache.commons.codec.binary.Base64;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;

/**
 * 图像数据输出
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Jul 31, 2012 lexloo
 * </pre>
 */
public class ImageDataValueExport implements IValueExport {
    @Override
    public PdfPCell cell2PdfCell(Cell cell, ReportModel model) {
        PdfPCell rtn = new PdfPCell();
        Object value = cell.getValue();
        if (value != null) {
            try {
                String base64Data = ((ImageValue) cell.getValue()).getData();
                if (StringUtils.isEmpty(base64Data)) {
                    rtn.addElement(new Paragraph(""));
                } else {
                    InputStream is = new ByteArrayInputStream(Base64.decodeBase64(base64Data));

                    if (is != null) {
                        BufferedImage image = ImageIO.read(is);
                        Image cellImg = Image.getInstance(image, null);
                        rtn.addElement(cellImg);
                        is.close();
                    } else {
                        rtn.addElement(new Paragraph(""));
                    }
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        rtn.setFixedHeight(model.getRowHeight(cell.getRow()));
        Style style = cell.getStyle();
        rtn.setVerticalAlignment(Element.ALIGN_TOP);
        // VerticalAlignment vAlign = style.getVerticalAlignment();
        //        
        // if (vAlign == VerticalAlignment.Top) {
        //
        // } else if (vAlign == VerticalAlignment.Bottom) {
        // rtn.setVerticalAlignment(Element.ALIGN_BOTTOM);
        // } else {
        // rtn.setVerticalAlignment(Element.ALIGN_MIDDLE);
        // }

        HorizentalAlignment hAlign = style.getHorizentalAlignment();
        if (hAlign == HorizentalAlignment.Left) {
            rtn.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else if (hAlign == HorizentalAlignment.Right) {
            rtn.setHorizontalAlignment(Element.ALIGN_RIGHT);
        } else {
            rtn.setHorizontalAlignment(Element.ALIGN_CENTER);
        }

        return rtn;
    }
}
